// xc_lshop/club/order/writeoff.js
var club_scanbill = require('../model/club_scan')
var actbill = require('../model/club_bill')
Page({
  mixins: [require('../mixins/clubMixin')],
  /**
   * 页面的初始数据
   */
  data: {
    scanconfig: {},
    scanmode: "6",
    isshowcamera: false,
    canscan: true,  //判断
    actmodel: null,
    arrival: [],
    writeoff: [],  
    canopening:false

  },
  afterclubinfo: function (options, config, userinfo, clubinfo) {
    var that = this;
    this.opscan();
   
  },
  onNavChange: function (e) {
    this.setData({ scanmode: e.detail.name })
  },
  onPopShow:function(e){
    var dataset=e.currentTarget.dataset
    var that=this;  
    that.selectComponent("#orderinfo").showmodel(dataset.item, that.data.scanconfig, that.data.scanmode)

  },
  opscan:function(e){
    var that= this;
      wx.scanCode({
      onlyFromCamera: false,
      success:function(reslut){
        var vcode=""
        if(reslut.result){
          vcode=reslut.result
        }
        else if( reslut.path){
          vcode=reslut.path        
        }
        else
        {
          return 
        }
        if(vcode)
        {
          //"0022", "2020202011112626112506"
          //that.deailerdata("0021_2020202011112323195147") 
          // that.deailerdata("0022_2121212103031010180111") 
          that.deailerdata(vcode) 
        }          
      },
      complete:function(com){
      
        
      }
    })  
  },
  deailerdata(vcode){
    var that = this;
    var arrvcode= vcode.split("_");
    console.log("arrvcode[0]")
    console.log(arrvcode)
    console.log(arrvcode[0])

    switch(arrvcode[0]){
      case "0021":{
        that.setData({scanmode:"4"})
        that.getmodel(arrvcode[1])        
        break;
      }
      case "0022":{
        that.setData({scanmode:"6"})
        that.getmodel(arrvcode[1])            
        break;
      }
     }
  },
  scancodechange: function (e) { 
    if (this.data.canscan) {
      this.data.canscan = false
      var resultval = e.detail.result
    }
  },
  getmodel: function (ordernum) {
    var that = this;
    actbill.getmodel(ordernum).then(function (resinfo) {
      if (resinfo["status"] == "1") {
        var model = resinfo["obj"];  
      
       that.comopaddmodel(that.data.scanmode,model)   
        that.selectComponent("#orderinfo").showmodel(model, that.data.scanconfig, that.data.scanmode)
      }
    })
  },
  cameraset() {
    var that = this;
    that.selectComponent("#cameraset").showmodel()
  },
  clearrecord(){
    var actfiled= this.data.scanmode=='4'?"arrival":"writeoff"
    var updatemodel={}
    updatemodel[actfiled]=[] 
     this.setData(updatemodel) 
  },
  scanconfigupdate(e) {
    this.setData({ scanconfig: e.detail })
  },
  updatecomop(e) {  
    var that = this;
    var para = e.detail 


    var updatemodel = {};
    switch (para["op"]) {
      case "scanmode": {
        //
        updatemodel["scanmode"] = para["scanmode"];
        updatemodel["actmodel"] = para["model"];
        that.setData(updatemodel)
        that.comopaddmodel(para["scanmode"], para["model"])
        break
      }
      case "bill": {
        
        that.updatebill(para["scanmode"], para["model"])
        break
      }
    }

  },
  updatebill(scanmodel, model) {
    var actfield = this.scanmodetostr(scanmodel);
    actlist = this.data[actfield];
    for (var a in actlist) {
      if (actlist[a]["ordernum"] == model.ordernum) {
        var updatemodel = {};
        updatemodel[actfield + "[" + a + "].status"] = model.status;
        updatemodel[actfield + "[" + a + "].clubop"] = model.clubop;

        that.setData(updatemodel);
        return
      }
    }
  },

  scanmodetostr(scanmodel) {
    var actfield = ""
    if (scanmodel == "4") {
      actfield = "arrival"
    }
    else if (scanmodel == "6") {
      actfield = "writeoff"
    }
    return actfield

  },
  comopaddmodel(scanmodel, model) {
    var actfield = this.scanmodetostr(scanmodel);
   var  actlist = this.data[actfield];
   var updatemodel={};
    for (var a in actlist) {
      if (actlist[a]["ordernum"] == model.ordernum) {
  
        updatemodel[actfield+"["+a+"]"]=model

        this.setData(updatemodel)
        return
      }
    }
  
    actlist.push(model)   
    updatemodel[actfield]=actlist;
    this.setData(updatemodel)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    club_scanbill.getconfig().then(function (res) {
      that.setData({ scanconfig: res })
    })
    // that.deailerdata("0022_2121212103031010180111") 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  
})